package org.uma.jmetal.runner.multiobjective.pls;

import java.util.List;

import org.uma.jmetal.algorithm.Algorithm;
import org.uma.jmetal.problem.multiobjective.GridManyObjectiveKnapsack;
import org.uma.jmetal.solution.GridPermutationSolution;
import org.uma.jmetal.util.AlgorithmRunner;
import org.uma.jmetal.util.JMetalLogger;
import org.uma.jmetal.util.fileoutput.SolutionListOutput;
import org.uma.jmetal.util.fileoutput.impl.DefaultFileOutputContext;

import com.nuaa.shr.pls.algorithm.KnapsackGridPls;
import com.nuaa.shr.pls.algorithm.KnapsackGridWeakDominatePls;
import com.nuaa.shr.pls.algorithm.KnapsackMoead;

public class KnapsackPlsRunner {
	public static void main(String[] args) {

		int[] itemNumber = { 250, 500, 750 };
		for (int itemIdx = 2; itemIdx < 3; itemIdx++) {

			GridManyObjectiveKnapsack problem = new GridManyObjectiveKnapsack(
					"/knapsack/profit" + itemNumber[itemIdx] + "_3.txt",
					"/knapsack/weight" + itemNumber[itemIdx] + "_3.txt",
					"/knapsack/capacity" + itemNumber[itemIdx] + "_3.txt", itemNumber[itemIdx], 3);

			int run = 30;
			for (int i = 0; i < run; i++) {
				Algorithm<List<GridPermutationSolution<Integer>>> algorithm = 
						new KnapsackGridWeakDominatePls(problem, 100, 300, 14);

				AlgorithmRunner runner = new AlgorithmRunner.Executor(algorithm).execute();
				List<GridPermutationSolution<Integer>> population = algorithm.getResult();
				long computingTime = runner.getComputingTime();

				new SolutionListOutput(population).setSeparator("\t")
						.setVarFileOutputContext(new DefaultFileOutputContext(
								"results/gplsd/knapsack" + itemNumber[itemIdx] + "-3-14D/VAR" + i + ".tsv"))
						.setFunFileOutputContext(new DefaultFileOutputContext(
								"results/gplsd/knapsack" + itemNumber[itemIdx] + "-3-14D/FUN" + i + ".tsv"))
						.print();

				JMetalLogger.logger.info("Total execution time: " + computingTime + "ms");
				JMetalLogger.logger.info("Objectives values have been written to file FUN.tsv");
				JMetalLogger.logger.info("Variables values have been written to file VAR.tsv");
			}
		}
	}
}
